package com.chinasofti.framework.dataaccess;

import java.lang.reflect.Type;
import java.util.HashMap;

import com.chinasofti.framework.base.Utils;
import com.chinasofti.framework.data.Entity;
import com.chinasofti.framework.data.FieldType;

public class Condition extends Entity{
	
	public enum Operation {
		GREATER_THAN, 
		EQUAL, 
		NOT_EQUAL,
		LESS_THAN, 
		INCLUDE, 
		IN,
		INVALIDATE
	}
	
	
	private static HashMap<String, FieldType> m_fields;
	private static final String ENTITY_NAME = "Condition";
	private static final String KEY_FIELD = "";
	
	public Condition(){
		init();
	}
	
	@Override
	protected void init() {
		if(m_fields == null){
			m_fields = new HashMap<String, FieldType>();
			m_fields.put("EntityType", new FieldType(Type.class));
			m_fields.put("Field", new FieldType(String.class));
			m_fields.put("Type", new FieldType(Type.class));
			m_fields.put("Operation", new FieldType(Operation.class));
			m_fields.put("Value", new FieldType(Object.class));
		}
	}
	
	@Override 
	public ConditionParser getParser() {
		return new ConditionParser();
	}
	@Override
	public HashMap<String, FieldType> getFieldTypes() {
		return m_fields;
	}
	
	@Override
	public  String getKeyField() {
		return KEY_FIELD;
	}
	
	@Override
	public String getEntityName() {
		return ENTITY_NAME;
	}
	
	public Condition (String field, Operation operation, Object value) {
		init();
		set("Field", field);
		set("Operation", operation);
		set("Value", value);
	}
	
	
	public String getField() {
		return getString("Field");
	}
	
	
	public Operation getOperation() {
		return (Operation)get("Operation");
	}
	
	public Object getValue() {
		return get("Value");
	}
	
	
	public String toSqlString() {
		Object obj = getValue();
		Type type = obj.getClass();
		String value = Utils.getSimpleString(obj);
		String field = getField();
		
		if(!type.equals(Integer.class) && !type.equals(Float.class)	
				&& !type.equals(Long.class) && !type.equals(Double.class) 
				&& !type.equals(Short.class) && !type.equals(boolean.class)) {
			value = "'" + value + "'";
		}

		String sql = "";
		switch (getOperation()) {
		case GREATER_THAN:
			sql = field + ">" + value;
			break;
		case LESS_THAN:
			sql = field + "<" + value;
			break;
		case EQUAL:
			sql = field + "=" + value;
			break;
		case INCLUDE:
			sql = field + " LIKE '%" + value + "%'";
			break;
		case IN:
			Object[] objs = (Object[])getValue();
			value += "(";
			for(int i = 0; i < objs.length; i++) {
				if(type == Integer.class || type == Float.class	
						|| type == Long.class|| type == Double.class) {
					if(i == 0) {
						value += objs[i].toString(); 
					}
					else {
						value += objs[i].toString() + ",";
					}
				}
				else {
					if(i == 0) {
						value += "'" + objs[i].toString() + "'"; 
					}
					else {
						value += "'" + objs[i].toString() + "'"  + ",";
					}
				}
			}
			value += ")";
			sql = field + " IN " + value;
		default:
			sql = "";
			break;
		}
		return sql;
	}

}
